﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.IdentityModel.OAuth;

namespace AMT.IdentityModel.OAuth.Tests
{
	[TestClass]
	public class TestSWTUserPasswordClaimToken
	{
		private TestParameters m_params;
		private SimpleWebToken m_swt;

		[TestInitialize]
		public void Initialize()
		{
			m_params = TestParameters.LoadFromConfig();
		}

		#region Valid (positive) tests
		[TestMethod]
		public void ValidUsername_ValidPassword()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wrapHandler = GetDefaultWrapHandler();
			Assert.IsNotNull(wrapHandler);
			wrapHandler.ClaimToken = new SWTUserPasswordClaimToken(m_params.IdentityName, m_params.IdentityPassword);

			Console.WriteLine(TestParameters.GetParamInfoForOutput(wrapHandler));
			m_swt = wrapHandler.RetrieveTokenFromSTS();
			Assert.IsNotNull(m_swt);
            Assert.IsTrue(m_params.STSBaseUri.ToString().Contains(m_swt.Issuer));
            Assert.AreEqual(m_params.Scope, m_swt.Audience);
		}
		#endregion


		#region Invalid (negative) tests
		[TestMethod]
		public void ValidUsername_InvalidPassword()
		{
			m_params.IdentityPassword += 'X';

			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wrapHandler = GetDefaultWrapHandler();
			Assert.IsNotNull(wrapHandler);

			wrapHandler.ClaimToken = new SWTUserPasswordClaimToken(m_params.IdentityName, m_params.IdentityPassword);

			try
			{
				Console.WriteLine(TestParameters.GetParamInfoForOutput(wrapHandler));
				m_swt = wrapHandler.RetrieveTokenFromSTS();
			}
			catch (System.Net.WebException ex)
			{
				System.Diagnostics.Debug.WriteLine(string.Format("Status: {0}", ex.Status.ToString()));
				System.Diagnostics.Debug.WriteLine(string.Format("Message: {0}", ex.Message));
                Assert.IsTrue(ex.Message.Contains("(401)"));
                Assert.IsTrue(ex.Message.Contains("Unauthorized"));
            }
		}


		[TestMethod]
		public void InvalidUsername_ValidPassword()
		{
			m_params.IdentityName += 'X';

			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wrapHandler = GetDefaultWrapHandler();
			Assert.IsNotNull(wrapHandler);

			wrapHandler.ClaimToken = new SWTUserPasswordClaimToken(m_params.IdentityName, m_params.IdentityPassword);

			try
			{
				Console.WriteLine(TestParameters.GetParamInfoForOutput(wrapHandler));
				m_swt = wrapHandler.RetrieveTokenFromSTS();
			}
			catch (System.Net.WebException ex)
			{
				System.Diagnostics.Debug.WriteLine(string.Format("Status: {0}", ex.Status.ToString()));
				System.Diagnostics.Debug.WriteLine(string.Format("Message: {0}", ex.Message));
				Assert.IsTrue(ex.Message.Contains("(401)"));
				Assert.IsTrue(ex.Message.Contains("Unauthorized"));
			}
		}
		#endregion

		#region Private helper methods
		private AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> GetDefaultWrapHandler()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wrapHandler =
						 new AMT.IdentityModel.OAuth.WRAPHandler<SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken>(
							 new Uri(m_params.STSBaseUri, m_params.WRAPEndpointPath), m_params.Scope);
			return wrapHandler;
		}
		#endregion
	}
}
